//导入模块
import {load} from "/code/admin/util/LoadView.js"
load("adduser")  
const addUsersBtn = document.getElementById("addUsersBtn")
const addUsersForm = document.getElementById("addUsersForm")
const Addusername = document.querySelector("#Addusername")
const introduction = document.querySelector("#introduction")
const Addpassword = document.querySelector("#Addpassword")
const userhead = document.querySelector("#userhead")
let photo =""
//给添加用户表单添加事件
addUsersForm.onsubmit =async function(evt){
    //阻止表单自动提交
    evt.preventDefault()
    //发送提交请求
    await fetch("http://localhost:3000/users",{
        method:"POST",
        headers:{
            "content-type": "application/json"
        },
        body:JSON.stringify({
            username:Addusername.value,
            password:Addpassword.value,
            instruction:introduction.value,
            photo
        })
    }).then(res => res.json())
    //跳转到用户列表页面
    location.href = "/code/admin/view/user-manage//userList/index.html"
}
//添加头像图片时，转换成base64格式
userhead.onchange = function(evt){
    let reader = new FileReader()
    reader.readAsDataURL(evt.target.files[0])
    reader.onload = function(e){
        photo = e.target.result
    }
    // console.log(photo)
}
